package com.wdd.atcrowdfunding.manager.controller;

import com.wdd.atcrowdfunding.bean.Tag;
import com.wdd.atcrowdfunding.manager.service.TagService;
import com.wdd.atcrowdfunding.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @RequestMapping("/index")
    public String index(){
        return "tag/index";
    }

    @RequestMapping("/toAdd")
    public String toAdd(String id){
        return "tag/add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Map map){
        Tag tag = tagService.getTagById(id);
        map.put("tag",tag);
        return "tag/update";
    }


    @RequestMapping("/loadData")
    @ResponseBody
    public AjaxResult loadData(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Tag> root = new ArrayList<Tag>();
            List<Tag> childredPermissons =  tagService.selectAll();
            Map<Integer,Tag> map = new HashMap<Integer,Tag>();//100
            for (Tag innerpermission : childredPermissons) {
                map.put(innerpermission.getId(), innerpermission);
            }
            for (Tag tag : childredPermissons) { //100
                //通过子查找父
                //子菜单
                Tag child = tag ; //假设为子菜单
                if(child.getPid() == null ){
                    root.add(tag);
                }else{
                    //父节点
                    Tag parent = map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }
            ajaxResult.setSuccess(true);
            ajaxResult.setData(root);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("加载许可树数据失败!");
        }


        return ajaxResult ;
    }


    @ResponseBody
    @RequestMapping("/doAdd")
    public AjaxResult doAdd(Tag tag){
        AjaxResult ajaxResult = new AjaxResult();

        try {

            int count = tagService.saveTag(tag);
            if(count==1){
                ajaxResult.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("保存许可树数据失败!");
        }
        return ajaxResult ;
    }


    @ResponseBody
    @RequestMapping("/deleteTag")
    public AjaxResult deletePermission(Integer id, HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();

        try {
            int count = tagService.deleteTag(id);
            if(count==1){
                ajaxResult.setSuccess(true);
            }

        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("删除许可树数据失败!");
        }
        return ajaxResult ;
    }



    @ResponseBody
    @RequestMapping("/doUpdate")
    public AjaxResult doUpdate(Tag tag,HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int count = tagService.updateTag(tag);

            ajaxResult.setSuccess(true);


        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("保存许可树数据失败!");
        }


        return ajaxResult ;
    }

}
